using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

namespace ResourceBlender_Express {
    /// <summary>
    /// Allows a user to specify the database mode and enter connection details.
    /// </summary>
    public partial class DatabaseSelector : UserControl {
        /// <summary>
        /// Initializes a new <see cref="DatabaseSelector"/>.
        /// </summary>
        public DatabaseSelector() {
            InitializeComponent();
        }

        /// <summary>
        /// Validates user input then returns a DialogResult of true.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        public bool ValidateSettings() {
            bool valid = true;
            if(cboDBMode.SelectedIndex == 0 || cboDBMode.SelectedIndex == 1) {
                txtDBHost.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                txtDBPort.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                txtDBUser.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                txtDBDatabase.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                txtDBPath.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            }
            Window parentWindow = Window.GetWindow(this);
            if(cboDBMode.SelectedIndex <= 1) {
                if(string.IsNullOrEmpty(txtDBHost.Text)) {
                    MessageBox.Show(parentWindow, "Database hostname must be entered", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtDBHost.Focus();
                    valid = false;
                } else if(string.IsNullOrEmpty(txtDBPort.Text)) {
                    MessageBox.Show(parentWindow, "Database port must be between 1 and 65536", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtDBPort.Focus();
                    valid = false;
                } else if(string.IsNullOrEmpty(txtDBUser.Text)) {
                    MessageBox.Show(parentWindow, "Database username must be entered", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtDBUser.Focus();
                    valid = false;
                } else if(string.IsNullOrEmpty(txtDBDatabase.Text)) {
                    MessageBox.Show(parentWindow, "Database name must be entered", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtDBDatabase.Focus();
                    valid = false;
                }
            } else if(cboDBMode.SelectedIndex == 2) {
                if(string.IsNullOrEmpty(txtDBPath.Text)) {
                    MessageBox.Show(parentWindow, "A database file is required", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtDBHost.Focus();
                    valid = false;
                }
            }
            return valid;
        }

        /// <summary>
        /// Changes an invisible tab control to switch between server details for MySQL/SQLServer and a filename for SQLite.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void cboDBMode_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            switch(cboDBMode.SelectedIndex) {
                case 0:
                case 1:
                    tbcModeFields.SelectedIndex = 0;
                    break;
                case 2:
                    tbcModeFields.SelectedIndex = 1;
                    break;
            }
        }

        /// <summary>
        /// Adds a selection changed handler to the mode combobox, then displays the tab for the current mode.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void DatabaseSelector_Loaded(object sender, RoutedEventArgs e) {
            cboDBMode.SelectionChanged += new SelectionChangedEventHandler(cboDBMode_SelectionChanged);
            cboDBMode_SelectionChanged(cboDBMode, null);
        }

        /// <summary>
        /// Shows a dialog to select a database file.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void BrowseForDBFile(object sender, RoutedEventArgs e) {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".db3";
            dlg.OverwritePrompt = false;
            dlg.CheckPathExists = true;
            dlg.FileName = txtDBPath.Text;
            dlg.Filter = "SQLite database files (.db3)|*.db3|All files|*.*";
            dlg.Title = "Database Path";
            if(dlg.ShowDialog() == true) {
                txtDBPath.Text = dlg.FileName;
            }
        }
    }
}
